/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 *  * Jason this is the latest code V1.0
 */

package edu.ipfw.cs590.cheat.services.utils.messages;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.Name;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPFactory;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPHeaderElement;
import javax.xml.soap.SOAPMessage;


/**
 *
 * @author rrm46582
 */
public abstract class CheatMessage
{
    static final private String NAMESPACE = "HTTP://cheat.cs590.ipfw.edu";
    protected SOAPMessage       message;
    protected SOAPHeader        header;
    protected SOAPBody          body;
    protected SOAPFactory       soapFactory;
    protected SOAPElement       serviceBody;

    protected CheatMessage(String token, String method)
    {
        try
        {
        MessageFactory messageFactory = MessageFactory.newInstance();
        soapFactory = SOAPFactory.newInstance();
        message = messageFactory.createMessage();
        header = message.getSOAPHeader();
        SOAPHeaderElement hElement = header.addHeaderElement(soapFactory.createName("Token", "tns", NAMESPACE));
        hElement.addTextNode(token);

		body = message.getSOAPBody();

		// Add a body element that says which service we are calling NOTE: This does not affect Servlets
		Name serviceName = soapFactory.createName(method, "tns", NAMESPACE);
        serviceBody = body.addBodyElement(serviceName);
        //addParams();
		//body.addBodyElement(serviceName);

        }
        catch( Exception e)
        {
            e.printStackTrace();
        }
    }

    protected void addParams()
    {
        try
        {
            Name paramName;
            Set<String> keySet = params().keySet();
            Iterator<String> iKey = keySet.iterator();

            while ( iKey.hasNext())
            {
                String p = iKey.next();
                paramName = soapFactory.createName( p );
                SOAPElement paramElement = serviceBody.addChildElement(paramName);
                paramElement.addTextNode(params().get(p));
            }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        // Add in the UUID

    }

    protected abstract String getURL();
    public abstract String getServiceName();
    public abstract HashMap<String, String> params();
    public abstract String call();

}
